package io.geekidea.springbootplus.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.annotations.CurrentAdmin;
import io.geekidea.springbootplus.system.annotations.CurrentAdminInfo;
import io.geekidea.springbootplus.system.annotations.CurrentUser;
import io.geekidea.springbootplus.system.entity.Schoolconfig;
import io.geekidea.springbootplus.system.param.SchoolconfigPageParam;
import io.geekidea.springbootplus.system.service.SchoolconfigService;
import io.geekidea.springbootplus.system.vo.SchoolconfigQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  控制器
 *
 * @author miko
 * @since 2020-11-08
 */
@Slf4j
@RestController
@RequestMapping("/schoolconfig")
@Module("system")
@Api(value = "API", tags = {""})
public class SchoolconfigControllerExtra extends BaseController {

    @Autowired
    private SchoolconfigService schoolconfigService;

    /**
     * 添加
     */
    @PostMapping("/getBySchool")
    @OperationLog(name = "获取", type = OperationLogType.LIST)
    @ApiOperation(value = "获取", response = ApiResult.class)
    public ApiResult<Schoolconfig> addSchoolconfig(@CurrentAdmin CurrentAdminInfo currentAdminInfo) throws Exception {
        int schoolID = currentAdminInfo.getSchool().getSchoolID();
        Schoolconfig schoolconfig = schoolconfigService.getOne(new LambdaQueryWrapper<Schoolconfig>().eq(Schoolconfig::getSchoolID,schoolID));
        return ApiResult.ok(schoolconfig);
    }

    /**
     * 更新
     */
    @PostMapping("/updateBySchool")
    @OperationLog(name = "更新", type = OperationLogType.LIST)
    @ApiOperation(value = "更新", response = ApiResult.class)
    public ApiResult<Schoolconfig> update(@CurrentAdmin CurrentAdminInfo currentAdminInfo,@RequestBody Schoolconfig schoolconfig) throws Exception {
        int schoolID = currentAdminInfo.getSchool().getSchoolID();
        schoolconfig.setSchoolID(schoolID);
        System.out.println(JSON.toJSONString(schoolconfig));
        schoolconfigService.saveOrUpdate(schoolconfig, new LambdaQueryWrapper<Schoolconfig>().eq(Schoolconfig::getSchoolID, schoolID));
        return ApiResult.ok(schoolconfig);
    }
}
